package com.asuka.eva.controller.provider;

import cn.hsa.cep.ivc.api.dto.StafPsnClctDetlDTO;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.asuka.eva.service.impl.DemoServiceImpl;
import io.itit.itf.okhttp.FastHttpClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author ZuoWei Jin, 13504284153
 * @version 1.0
 * @since 2021/10/3 22:50
 */
@RestController
@Slf4j
public class DemoController {

    @Resource
    DemoServiceImpl demoServiceImpl;

    @PostMapping(value = "/testThread")
    public void testThread() {
        demoServiceImpl.executeTaskSimple();
    }

    @PostMapping(value = "/testPage")
    public void testPage() {
        TimeInterval timer = DateUtil.timer();
        StafPsnClctDetlDTO stafPsnClctDetlDTO = new StafPsnClctDetlDTO();
        stafPsnClctDetlDTO.setEmpNo("21000021020071722852");
        stafPsnClctDetlDTO.setClctBillId("8312544");
        stafPsnClctDetlDTO.setPageNo(1);
        stafPsnClctDetlDTO.setPageSize(1000);
        boolean isLastPage = false;
        while (!isLastPage) {
            timer.restart();
            log.info("queryStafPsnClctDetlList is running for page no: {}", stafPsnClctDetlDTO.getPageNo());
            JSONObject res = queryStafPsnClctDetlList(stafPsnClctDetlDTO);
            stafPsnClctDetlDTO.setPageNo(stafPsnClctDetlDTO.getPageNo() + 1);
            if (res == null) {
                log.error("queryStafPsnClctDetlList is null");
                break;
            }
            log.info("queryStafPsnClctDetlList is end for time(s):{},time(ms):{}", timer.intervalSecond(), timer.intervalMs());
            isLastPage = (Boolean)res.getObject("data", Map.class).get("lastPage");
        }
    }

    private JSONObject queryStafPsnClctDetlList(StafPsnClctDetlDTO stafPsnClctDetlDTO) {
        String httpUrl = "http://10.43.33.13:8080/ivc/api/payinfomgr/queryStafPsnClctDetlList";
        JSONObject jsonObject = null;
        try {
            String response = FastHttpClient.post()
                    .url(httpUrl)
                    .body(JSON.toJSONString(stafPsnClctDetlDTO))
                    .build().execute().string();
            jsonObject = JSON.parseObject(response);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
}
